﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADTool.Jig
{
    public class RectangleJig : EntityJig
    {
        public Point3d _startPoint;
        public Point3d _endPoint;
        public RectangleJig(Point3d startPoint, Point3d endPoint) :
            base(new Polyline())
        {
            (Entity as Polyline).AddVertexAt(0, new Point2d(_startPoint.X, _startPoint.Y), 0, 0, 0);//左下角
            (Entity as Polyline).AddVertexAt(1, new Point2d(_startPoint.X, _startPoint.Y), 0, 0, 0);//右下角
            (Entity as Polyline).AddVertexAt(1, new Point2d(_startPoint.X, _startPoint.Y), 0, 0, 0);//右上角
            (Entity as Polyline).AddVertexAt(1, new Point2d(_startPoint.X, _startPoint.Y), 0, 0, 0);//左上角
            (Entity as Polyline).Closed = true;
            _startPoint = startPoint;
            _endPoint = endPoint;
        }
        public Entity GetEntity()
        {
            return Entity;
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions opts = new JigPromptPointOptions("\n请输入矩形终点");
            PromptPointResult ppr = prompts.AcquirePoint(opts);
            if (ppr.Status == PromptStatus.OK)
            {
                _endPoint = ppr.Value;
                return SamplerStatus.NoChange;
            }
            return SamplerStatus.Cancel;
        }
        protected override bool Update()
        {
            Polyline poly = (Entity as Polyline);
            poly.SetPointAt(0, new Point2d(_startPoint.X, _startPoint.Y)); // 左下角
            poly.SetPointAt(1, new Point2d(_endPoint.X, _startPoint.Y)); // 右下角
            poly.SetPointAt(2, new Point2d(_endPoint.X, _endPoint.Y)); // 右上角
            poly.SetPointAt(3, new Point2d(_startPoint.X, _endPoint.Y)); // 左上角
            return true;
        }
    }
}
